package com.exam.netease;

import java.util.Scanner;

/**
 * date:
 * description:
 */
public class Solution004 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[][] grid = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                grid[i][j] = scanner.nextInt();
            }
        }
        int[][] dp = new int[n][m];
        for (int j = 1; j < m; j++) {
            if(grid[0][j] == grid[0][j - 1]){
                dp[0][j] = dp[0][j - 1] + 1;
            }else{
                dp[0][j] = dp[0][j - 1] + 2;
            }
        }
        for (int i = 1; i < n; i++) {
            if(grid[i][0] == grid[i - 1][0]){
                dp[i][0] = dp[i - 1][0] + 1;
            }else{
                dp[i][0] = dp[i - 1][0] + 2;
            }
        }
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < m; j++) {
                int up = dp[i - 1][j];
                int left = dp[i][j - 1];
                if(grid[i][j] == grid[i - 1][j]){
                    up += 1;
                }else{
                    up += 2;
                }
                if(grid[i][j] == grid[i][j - 1]){
                    left += 1;
                }else{
                    left += 2;
                }
                dp[i][j] = Math.min(up, left);
            }
        }
        System.out.println(dp[n - 1][m - 1]);
    }
}
